﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ch.froorider.NGoogleCalendar.facade.businessObjects;
using ch.froorider.NGoogleCalendar.persistence;
using ch.froorider.NGoogleCalendar.tools;

namespace NGoogleCalendarTests
{
    /// <summary>
    /// Summary description for PersistenceManagerTests
    /// </summary>
    [TestClass]
    public class PersistenceManagerTests
    {
        #region fields

        private static User theUser = new User();

        private TestContext testContextInstance;
        
        #endregion

        #region Properties

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #endregion

        #region Constructors // Inits etc.

       
         //Use ClassInitialize to run code before running the first test in the class
         [ClassInitialize()]
         public static void MyClassInitialize(TestContext testContext) 
         {
             theUser.AccountName = "foo@bar.com";
             theUser.Password = "someSecretPassword";
             theUser.UserName = "foobar";
         }

        #endregion

        #region Additional test attributes
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        #region Tests

        /// <summary>
        /// Test's if an Business object can be serialized
        /// </summary>
        [TestMethod]
        public void SerializeTest()
        {
            User expectedUser = new User()
            {
                AccountName = "khfkajshf",
                Password = "vöjlen.,fv",
                UserName = "oiafdnmwea"
            };
            string filename = expectedUser.Serialize();
           Assert.IsNotNull(filename);
           Assert.AreEqual(filename, expectedUser.ObjectIdentifier);
        }

        /// <summary>
        /// Test's if a serialized object can be deserialized.
        /// </summary>
        [TestMethod]
        public void DeserializeTest()
        {
            User expectedUser = new User()
            {
                AccountName = "oiaaenmchxvkj",
                Password = "98fdasgl",
                UserName = "478590543jkhlkjhnvm"
            };
            User actualUser;
            User targetUser;

            string filename = expectedUser.Serialize();
            actualUser = PersistenceManager.DeserializeObject<User>(filename);
            actualUser.ObjectIdentifier = filename;

            targetUser = expectedUser;
            Assert.AreEqual<User>(targetUser, expectedUser);
            Assert.IsTrue(expectedUser.Equals(actualUser));
            Assert.AreEqual<User>(expectedUser, actualUser);
        }

        #endregion
    }
}
